/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.source.web.floor;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.source.entity.floor.EleSourceFloor;
import com.thinkgem.jeesite.modules.source.entity.floor.FloorNode;
import com.thinkgem.jeesite.modules.source.service.floor.EleSourceFloorService;
import com.thinkgem.jeesite.modules.source.service.floor.FloorNodeService;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 楼层资源管理Controller
 * @author ZC
 * @version 2017-06-22
 */
@Controller
@RequestMapping(value = "${adminPath}/source/floor/eleSourceFloor")
public class EleSourceFloorController extends BaseController {

	@Autowired
	private EleSourceFloorService eleSourceFloorService;
	@Autowired
	private FloorNodeService floorNodeService;
	
	@ModelAttribute
	public EleSourceFloor get(@RequestParam(required=false) String id) {
		EleSourceFloor entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eleSourceFloorService.get(id);
		}
		if (entity == null){
			entity = new EleSourceFloor();
		}
		return entity;
	}
	
	@RequiresPermissions("source:floor:eleSourceFloor:view")
	@RequestMapping(value = {"list", ""})
	public String list(EleSourceFloor eleSourceFloor, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EleSourceFloor> page = eleSourceFloorService.findPage(new Page<EleSourceFloor>(request, response), eleSourceFloor); 
		model.addAttribute("page", page);
		return "modules/source/floor/eleSourceFloorList";
	}

	@RequiresPermissions("source:floor:eleSourceFloor:view")
	@RequestMapping(value = "form")
	public String form(EleSourceFloor eleSourceFloor, Model model) {
		eleSourceFloor.setFloornodes(floorNodeService.getByGroupId(eleSourceFloor.getId()));
		model.addAttribute("eleSourceFloor", eleSourceFloor);
		return "modules/source/floor/eleSourceFloorForm";
	}

	@RequiresPermissions("source:floor:eleSourceFloor:edit")
	@RequestMapping(value = "save")
	public String save(EleSourceFloor eleSourceFloor, Model model, RedirectAttributes redirectAttributes) {
		for(FloorNode fn:eleSourceFloor.getFloornodes()){
			fn.setGroupId(eleSourceFloor.getId());
		}
		if (!beanValidator(model, eleSourceFloor)){
			return form(eleSourceFloor, model);
		}
		eleSourceFloorService.save(eleSourceFloor);
		addMessage(redirectAttributes, "保存楼层资源成功");
		return "redirect:"+Global.getAdminPath()+"/source/floor/eleSourceFloor/?repage";
	}
	
	@RequiresPermissions("source:floor:eleSourceFloor:edit")
	@RequestMapping(value = "delete")
	public String delete(EleSourceFloor eleSourceFloor, RedirectAttributes redirectAttributes) {
		eleSourceFloorService.delete(eleSourceFloor);
		addMessage(redirectAttributes, "删除楼层资源成功");
		return "redirect:"+Global.getAdminPath()+"/source/floor/eleSourceFloor/?repage";
	}

}